﻿#ifndef BUSINESSVIEWMODEL_H
#define BUSINESSVIEWMODEL_H

#include <QObject>
#include <nlohmann/json.hpp>
#include "../userlistwidget.h"
#include "../mainwindow.h"
#include <UserManager/usermanager.h>
#include <protocol/protocol.h>

class BusinessViewModel : public QObject
{
    Q_OBJECT
public:


    // 删除拷贝构造函数和拷贝赋值操作符
    BusinessViewModel(BusinessViewModel const&) = delete;
    BusinessViewModel& operator=(BusinessViewModel const&) = delete;
    // 提供一个全局唯一的接口
    static BusinessViewModel& getInstance() {
        static BusinessViewModel instance;
        return instance;
    }
    // update user list
    void updateUserList(nlohmann::json & users);


    // add a message to user|args1: from who args2 : message is what
    void addMessageFromUser(const User &user, const std::string& message, int messageType, const std::string& time = "");

    void addMessageFromSelf(const User& user, const std::string& message, int messageType, const std::string& time = "");

    // add a message to group
    void addMessageFromGroup(const User&user, uint64_t groupID, const std::string& message, int messageType, const std::string& time = "");

    // void Updated file transfer information
    void updateFileTransferInfo(const std::string& info);

    // update file List
    void updateFileList(nlohmann::json & files);

    // update status bar text
    void updateStatusbarText(std::shared_ptr<Protocol> protocol);

    // update group lsit
    void updateGroupList(const nlohmann::json& groups);

    void updateGroupRequestList(const nlohmann::json& requests);
    // update status bar permanent text
    void updateStatusbarPermanentText(const std::string& message, bool connected);

    QString convertToUTF16(const std::string& input);
signals:

    void addMessage(int avatar, QString account, QString message, QString time, int messageType);
    // update mainwindow avatar and account
    void initUserLoginMessage();
    // Updated file transfer information
    void SignalUpdateFileTransferInfo(QString info);
    // add a file info to file list
    void addFileInfoToFileList(QString fileInfo);
    // update
    void signalUpdateStatusbarText(QString message);
    // add item to userList
    void userListAddItem(QString account);
    // add item to friendRequestList
    void requestListAddItem(QString account);
    // add a group item
    void addGroupItem(QString groupName);
    // add a groupMessage
    void addGroupMessage(int avatar, QString account, uint64_t groupID, QString message, QString time, int messageType);
    // clear the request list
    void clearGroupRequestList();
    // add a item to group request list
    void addGroupRequestItem(int avatar, QString userAccount, QString groupName);

    void SIGaddMessageFromSelf(QString targetAccount, QString message, QString time, int messageType);
    // update statusbar permanent text in mainwindow
    void SIGupdateStatusbarPermanentText(QString, bool);


private:

    MainWindow* mainWindow;

    BusinessViewModel();
};

#endif // BUSINESSVIEWMODEL_H
